/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minidota.model;

import com.minidota.utils.Util;


/**
 *
 * @author custom
 */
public class Vector2 {
    private double x;
    private double y;
    
    public Vector2() {        
    }
    
    public Vector2(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public Vector2(Vector2 toCopy) {
        this(toCopy.x, toCopy.y);
    }
    
    public final Vector2 add(Vector2 v) {
        return new Vector2(x + v.x, y + v.y);
    }
    
    public final Vector2 sub(Vector2 v) {
        return new Vector2(x - v.x, y - v.y);
    }
    
    public final Vector2 mul(double a) {
        return new Vector2(x * a, y * a);
    }
    
    public final Vector2 negate() {
        return new Vector2(-x, -y);
    }
    
    public void setZero() {
        x = 0;
        y = 0;
    }
    
    public boolean isZero() {
        return x == 0 && y == 0;
    }
    
    public final double length() {
        return Math.sqrt(x * x + y * y);
    }
    
    public final double lengthSquared() {
        return x * x + y * y;
    }
    
    public final Vector2 normalize() {
        return toLength(1);
    }
    
    public final Vector2 toLength(double nl) {
        double length = length();
        if (length > 0) {
            return (new Vector2(this)).mul(nl/length);
        } else {
            return new Vector2();
        }
    }
    
    public Vector2 rotate(int angle) {
        return new Vector2(x * Util.cos(angle) - y * Util.sin(angle)
                , x * Util.sin(angle) + y * Util.cos(angle));
    }
    
    public int calcVector2Angle(double length) {
        if (length < 0) {
            return 0;
        }
        int vAngle = (int) Math.round((y < 0 ? -1 : 1) * Math.acos(x / length) * 180 / Math.PI);
        return Util.fixAngle(vAngle);
    }
    
    public int calcVector2Angle() {
        return calcVector2Angle(length());
    }
    
    public double scalarProduct(Vector2 v) {
        return this.x * v.x + this.y * v.y;
    }

    /**
     * @return the x
     */
    public double getX() {
        return x;
    }

    /**
     * @return the y
     */
    public double getY() {
        return y;
    }
    
}
